﻿namespace SomethingSpacial
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Runtime.Serialization;
    using System.Collections.Generic;
    using SomethingSpacial.ViewModel;
    using SomethingSpacial.AppServices;
    using SomethingSpacial.AppServices.Resources;
    using SomethingSpacial.AppServices.Web;
    using System.ServiceModel.DomainServices.Client.ApplicationServices;


    public partial class App : Application
    {
        public static ExternalFeed MainFeed = new ExternalFeed();
        
        public static IDictionary<string, string> Settings = null;
        private Activity progressIndicator;

        public App()
        {
            InitializeComponent();
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {

            Settings = e.InitParams;

            string URL = System.Windows.Browser.HtmlPage.Document.DocumentUri.AbsoluteUri;
            MainFeed.Address = URL.Substring(0, URL.LastIndexOf('/')) + "/ServiceProxy.aspx?ServicePath=" + Settings["MainRSSFeed"];

            Settings["Newsfeed"] = URL.Substring(0, URL.LastIndexOf('/')) + "/ServiceProxy.aspx?ServicePath=" + Settings["Newsfeed"];

            //MainFeed.Address = Settings["MainRSSFeed"];

           // SetView(Settings["RootVisual"]);


            // This will enable you to bind controls in XAML files to WebContext.Current
            // properties
            this.Resources.Add("WebContext", SomethingSpacial.AppServices.WebContext.Current);
            // With the WebContext in a different library the AuthenticationContext cannot be found automatically
            //ds 0421 ((WebAuthenticationService)AppServices.WebContext.Current.Authentication).DomainContext = new AuthenticationContext();

            // This will automatically authenticate a user when using windows authentication
            // or when the user chose "Keep me signed in" on a previous login attempt
            //ds 0421 SomethingSpacial.AppServices.WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);





            // Show some UI to the user while LoadUser is in progress
            this.InitializeRootVisual();
        }


        /// <summary>
        /// designed to set the view.  SL3 disabled this to some degree and needs to be moved to a Shell method for view switching.
        /// </summary>
        /// <param name="ViewKey"></param>
        public static void SetView(string ViewKey)
        {
            switch (ViewKey)
            {
                case "Default":
                    //App.Current.RootVisual = new DefaultView();
                    App.Current.RootVisual = new SomethingSpacial.Views.Admin.AdminContainer();                    
                    break;
                case "LoginForm":
                    //App.Current.RootVisual = new LoginForm();
                    break;
            }
        }

        /// <summary>
        ///     Invoked when the <see cref="LoadUserOperation"/> completes. Use this
        ///     event handler to switch from the "loading UI" you created in
        ///     <see cref="InitializeRootVisual"/> to the "application UI"
        /// </summary>
        private void Application_UserLoaded(LoadUserOperation operation)
        {
            this.progressIndicator.IsActive = false;
        }

        /// <summary>
        ///     Initializes the <see cref="Application.RootVisual"/> property. The
        ///     initial UI will be displayed before the LoadUser operation has completed
        ///     (The LoadUser operation will cause user to be logged automatically if
        ///     using windows authentication or if the user had selected the "keep
        ///     me signed in" option on a previous login).
        /// </summary>
        protected virtual void InitializeRootVisual()
        {
            this.progressIndicator = new Activity();
            //this.progressIndicator.Content = new MainPage();

            //this.progressIndicator.Content = new Views.Default.DefaultView() ;
           // this.progressIndicator.Content = new Views.BeamMeUp.BeamMeUpContainer();

            //this.progressIndicator.Content = new Views.BeamMeUp.BeamMeUpContainerMain();
            
            
            //this.progressIndicator.Content = new SomethingSpacial.Views.MixItUp.MainPage();


//            SetView(Settings["RootVisual"]);

            switch (Settings["RootVisual"])
            {
                case "default":
                    this.progressIndicator.Content = new SomethingSpacial.Views.MixItUp.BasePage();
                    break;
                case "admin":
                    this.progressIndicator.Content = new Views.Admin.AdminContainer();
                    break;
            }
            //Admin Section

            //Mix It Up theme
           // this.progressIndicator.Content = new SomethingSpacial.Views.MixItUp.BasePage();

            //this.progressIndicator.IsActive = true;
            this.progressIndicator.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            this.progressIndicator.VerticalContentAlignment = VerticalAlignment.Stretch;

            // Let the user now we're trying to authenticate him
            this.progressIndicator.ActiveContent = ApplicationStrings.ActivityLoadingUser;

            this.RootVisual = this.progressIndicator;
        }

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // a ChildWindow control.
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                // NOTE: This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                e.Handled = true;
                ErrorWindow.CreateNew(e.ExceptionObject);
            }
        }
    }
}